home *** CD-ROM | disk | FTP | other *** search
Wrap
# Source Generated with Decompyle++ # File: in.pyo (Python 2.5) _copyright = 'Copyright 2001, Zolera Systems Inc. All Rights Reserved.\nCopyright 2001, MIT. All Rights Reserved.\n\nDistributed under the terms of:\n Python 2.0 License or later.\n http://www.python.org/2.0.1/license.html\nor\n W3C Software License\n http://www.w3.org/Consortium/Legal/copyright-software-19980720\n' import string from xml.dom import Node try: from xml.ns import XMLNS except: class XMLNS: BASE = 'http://www.w3.org/2000/xmlns/' XML = 'http://www.w3.org/XML/1998/namespace' try: import cStringIO StringIO = cStringIO except ImportError: import StringIO _attrs = lambda E: if not E.attributes or E.attributes.values(): pass[] _children = lambda E: if not E.childNodes: pass[] _IN_XML_NS = lambda n: n.name.startswith('xmlns') _inclusive = lambda n: n.unsuppressedPrefixes == None (_LesserElement, _Element, _GreaterElement) = range(3) def _sorter(n1, n2): i = cmp(n1.namespaceURI, n2.namespaceURI) if i: return i return cmp(n1.localName, n2.localName) def _sorter_ns(n1, n2): if n1[0] == 'xmlns': return -1 if n2[0] == 'xmlns': return 1 return cmp(n1[0], n2[0]) def _utilized(n, node, other_attrs, unsuppressedPrefixes): if n.startswith('xmlns:'): n = n[6:] elif n.startswith('xmlns'): n = n[5:] if (n == '' or node.prefix in ('#default', None)) and n == node.prefix or n in unsuppressedPrefixes: return 1 for attr in other_attrs: if n == attr.prefix: return 1 continue if unsuppressedPrefixes is not None: for attr in _attrs(node): if n == attr.prefix: return 1 continue return 0 def _inclusiveNamespacePrefixes(node, context, unsuppressedPrefixes): inclusive = [] if node.prefix: usedPrefixes = [ 'xmlns:%s' % node.prefix] else: usedPrefixes = [ 'xmlns'] for a in _attrs(node): if a.nodeName.startswith('xmlns') or not (a.prefix): continue usedPrefixes.append('xmlns:%s' % a.prefix) unused_namespace_dict = { } for attr in context: n = attr.nodeName if n in unsuppressedPrefixes: inclusive.append(attr) continue if n.startswith('xmlns:') and n[6:] in unsuppressedPrefixes: inclusive.append(attr) continue if n.startswith('xmlns') and n[5:] in unsuppressedPrefixes: inclusive.append(attr) continue if attr.nodeName in usedPrefixes: inclusive.append(attr) continue if n.startswith('xmlns:'): unused_namespace_dict[n] = attr.value continue return (inclusive, unused_namespace_dict) _in_subset = lambda subset, node: if not subset is None: passnode in subset class _implementation: handlers = { } def __init__(self, node, write, **kw): self.write = write self.subset = kw.get('subset') self.comments = kw.get('comments', 0) self.unsuppressedPrefixes = kw.get('unsuppressedPrefixes') nsdict = kw.get('nsdict', { 'xml': XMLNS.XML, 'xmlns': XMLNS.BASE }) self.state = (nsdict, { 'xml': '' }, { }, { }) if node.nodeType == Node.DOCUMENT_NODE: self._do_document(node) elif node.nodeType == Node.ELEMENT_NODE: self.documentOrder = _Element if not _inclusive(self): (inherited, unused) = _inclusiveNamespacePrefixes(node, self._inherit_context(node), self.unsuppressedPrefixes) self._do_element(node, inherited, unused = unused) else: inherited = self._inherit_context(node) self._do_element(node, inherited) elif node.nodeType == Node.DOCUMENT_TYPE_NODE: pass else: raise TypeError, str(node) def _inherit_context(self, node): xmlattrs = filter(_IN_XML_NS, _attrs(node)) inherited = [] parent = node.parentNode while parent and parent.nodeType == Node.ELEMENT_NODE: for a in filter(_IN_XML_NS, _attrs(parent)): n = a.localName if n not in xmlattrs: xmlattrs.append(n) inherited.append(a) continue parent = parent.parentNode return inherited def _do_document(self, node): self.documentOrder = _LesserElement for child in node.childNodes: if child.nodeType == Node.ELEMENT_NODE: self.documentOrder = _Element self._do_element(child) self.documentOrder = _GreaterElement continue if child.nodeType == Node.PROCESSING_INSTRUCTION_NODE: self._do_pi(child) continue if child.nodeType == Node.COMMENT_NODE: self._do_comment(child) continue if child.nodeType == Node.DOCUMENT_TYPE_NODE: continue raise TypeError, str(child) handlers[Node.DOCUMENT_NODE] = _do_document def _do_text(self, node): if not _in_subset(self.subset, node): return None s = string.replace(node.data, '&', '&') s = string.replace(s, '<', '<') s = string.replace(s, '>', '>') s = string.replace(s, '\r', '
') if s: self.write(s) handlers[Node.TEXT_NODE] = _do_text handlers[Node.CDATA_SECTION_NODE] = _do_text def _do_pi(self, node): if not _in_subset(self.subset, node): return None W = self.write if self.documentOrder == _GreaterElement: W('\n') W('<?') W(node.nodeName) s = node.data if s: W(' ') W(s) W('?>') if self.documentOrder == _LesserElement: W('\n') handlers[Node.PROCESSING_INSTRUCTION_NODE] = _do_pi def _do_comment(self, node): if not _in_subset(self.subset, node): return None if self.comments: W = self.write if self.documentOrder == _GreaterElement: W('\n') W('<!--') W(node.data) W('-->') if self.documentOrder == _LesserElement: W('\n') handlers[Node.COMMENT_NODE] = _do_comment def _do_attr(self, n, value): W = self.write W(' ') W(n) W('="') s = string.replace(value, '&', '&') s = string.replace(s, '<', '<') s = string.replace(s, '"', '"') s = string.replace(s, '\t', ' ') s = string.replace(s, '\n', '
') s = string.replace(s, '\r', '
') W(s) W('"') def _do_element(self, node, initial_other_attrs = [], unused = None): ns_parent = self.state[0] ns_rendered = self.state[1].copy() xml_attrs = self.state[2].copy() ns_unused_inherited = unused if unused is None: ns_unused_inherited = self.state[3].copy() ns_local = ns_parent.copy() inclusive = _inclusive(self) xml_attrs_local = { } other_attrs = [] in_subset = _in_subset(self.subset, node) for a in initial_other_attrs + _attrs(node): if a.namespaceURI == XMLNS.BASE: n = a.nodeName if n == 'xmlns:': n = 'xmlns' ns_local[n] = a.nodeValue elif a.namespaceURI == XMLNS.XML: if (inclusive or in_subset) and _in_subset(self.subset, a): xml_attrs_local[a.nodeName] = a elif _in_subset(self.subset, a): other_attrs.append(a) xml_attrs.update(xml_attrs_local) W = self.write name = None if in_subset: name = node.nodeName if not inclusive: if node.prefix is not None: prefix = 'xmlns:%s' % node.prefix else: prefix = 'xmlns' if not ns_rendered.has_key(prefix) and not ns_local.has_key(prefix): if not ns_unused_inherited.has_key(prefix): raise RuntimeError, 'For exclusive c14n, unable to map prefix "%s" in %s' % (prefix, node) ns_local[prefix] = ns_unused_inherited[prefix] del ns_unused_inherited[prefix] W('<') W(name) ns_to_render = [] for n, v in ns_local.items(): if n == 'xmlns' and v in [ XMLNS.BASE, ''] and ns_rendered.get('xmlns') in [ XMLNS.BASE, '', None]: continue if n in ('xmlns:xml', 'xml') and v in ('http://www.w3.org/XML/1998/namespace',): continue if (n, v) not in ns_rendered.items(): if inclusive or _utilized(n, node, other_attrs, self.unsuppressedPrefixes): ns_to_render.append((n, v)) elif not inclusive: ns_unused_inherited[n] = v _utilized(n, node, other_attrs, self.unsuppressedPrefixes) ns_to_render.sort(_sorter_ns) for n, v in ns_to_render: self._do_attr(n, v) ns_rendered[n] = v if not inclusive or _in_subset(self.subset, node.parentNode): other_attrs.extend(xml_attrs_local.values()) else: other_attrs.extend(xml_attrs.values()) other_attrs.sort(_sorter) for a in other_attrs: self._do_attr(a.nodeName, a.value) W('>') state = self.state self.state = (ns_local, ns_rendered, xml_attrs, ns_unused_inherited) for c in _children(node): _implementation.handlers[c.nodeType](self, c) self.state = state if name: W('</%s>' % name) handlers[Node.ELEMENT_NODE] = _do_element def Canonicalize(node, output = None, **kw): if output: apply(_implementation, (node, output.write), kw) else: s = StringIO.StringIO() apply(_implementation, (node, s.write), kw) return s.getvalue()